﻿using System;
using RemoteTestRunner.BusinessLogic;
using RemoteTestRunner.BusinessLogic.ViewModels;
using RemoteTestRunner.BusinessLogic.Views;

namespace RemoteTestRunner.FormsUI.Wpf
{
    public class ApplicationNavigationService : IApplicationNavigationService
    {
        #region Private Attributes

        private readonly IFileOpenDialogService _fileOpenDialogService;
        private readonly IFolderBrowserDialogService _folderBrowserDialogService;
        private readonly IRtrSettings _rtrSettings;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ApplicationNavigationService"/> class.
        /// </summary>
        /// <param name="fileOpenDialogService">The file open dialog service.</param>
        /// <param name="folderBrowserDialogService">The folder browser dialog service.</param>
        /// <param name="rtrSettings">The RTR settings.</param>
        public ApplicationNavigationService(IFileOpenDialogService fileOpenDialogService, IFolderBrowserDialogService folderBrowserDialogService, IRtrSettings rtrSettings)
        {
            _fileOpenDialogService = fileOpenDialogService;
            _folderBrowserDialogService = folderBrowserDialogService;
            _rtrSettings = rtrSettings;
        }

        #endregion

        #region IApplicationNavigationService Members

        public void ShowOptions(object dataContext)
        {
            var viewModel = dataContext as OptionsViewModel;
            if (viewModel == null)
            {
                throw new ArgumentNullException();
            }

            var optionsWindow = new Options { DataContext = dataContext };

            viewModel.Start(optionsWindow, _fileOpenDialogService, _folderBrowserDialogService, _rtrSettings);
            optionsWindow.ShowDialog();
        }

        public void ShowProjectAdmin(object dataContext)
        {
            var viewModel = dataContext as ProjectAdminViewModel;
            if (viewModel == null)
            {
                throw new ArgumentNullException();
            }

            var projectAdminWindow = new ProjectAdmin { DataContext = dataContext };
            viewModel.Start(projectAdminWindow);
            projectAdminWindow.ShowDialog();
        }

        public void ShowAbout(AboutViewModel viewModel)
        {
            var dlg = new About { DataContext = viewModel };

            dlg.ShowDialog();
        }

        public void ViewDataSource(string dataSourcePath)
        {
            System.Diagnostics.Process.Start(dataSourcePath);
        }

        /// <summary>
        /// Displays the Remote Test Runner (RTR) on-line help.
        /// </summary>
        public void DisplayOnlineHelp()
        {
            System.Diagnostics.Process.Start(@"http://rtr.codeplex.com/wikipage?title=A%20Guide%20to%20Using%20Remote%20Test%20Runner");
        }

        #endregion
    }
}
